package com.atguigu.flink03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * @author Felix
 * @date 2024/2/20
 * 该案例演示了自定义数据源
 *      定义一个类，实现SourceFunction接口
 *      将SourceFunction实现类对象作为参数传递给env.addSource方法
 */
public class Flink07_Source_Self {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        //StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        env.addSource(new MySource()).print();

        env.execute();
    }
}

class MySource implements SourceFunction<String>{

    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        //读取数据的逻辑
        for (int i = 0; i < 10000000; i++) {
            //将数据向下游传递
            ctx.collect("数据--" + i);
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        //job取消后执行的方法
        System.out.println("~~~结束~~~");
    }
}
